package com.bbm.communitysystem.tools;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DemoDate {

    public static String getNow() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        return simpleDateFormat.format(new Date());
    }

    public static String getDate(String date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format(Long.parseLong(date));
    }


    public static String getDay(String date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd");
        return simpleDateFormat.format(Long.parseLong(date));
    }

    public static String getHouse(long date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
        return simpleDateFormat.format(date);
    }

    //判断日期是否是昨天
    public static boolean isYesterday(long date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date now = new Date();
        Date yesterday = new Date(now.getTime() - 1000 * 60 * 60 * 24);
        String yesterdayStr = simpleDateFormat.format(yesterday);
        String nowStr = simpleDateFormat.format(now);

        return yesterdayStr.equals(simpleDateFormat.format(date)) && !nowStr.equals(simpleDateFormat.format(date));
    }

    /*
      时间范围
      1分钟内 刚刚
      1小时内 xx分钟前
      大于1小时 xx小时前

      昨天 MM:ss
      1周内 xx天前
      大于周 MM-dd
      大于年 yyyy-MM-dd
     */
    public static String getPostDate(String time) {
        String endDate = "";
        long l = System.currentTimeMillis();

        long createTime = Long.parseLong(time);

        if (DemoDate.isYesterday(createTime)) {

            endDate = "昨天\t" + DemoDate.getHouse(createTime);

            if (l - createTime > 60L * 60 * 1000 * 24 * 2 && l - createTime < 60L * 60 * 1000 * 24 * 7) {
                endDate = (l - createTime) / 1000 / 60 / 60 / 24 + "天前";
            }

            if (l - createTime > 60L * 60 * 1000 * 24 * 7) {
                endDate = DemoDate.getDay(time);
            }

            if (l - createTime > 60L * 60 * 1000 * 24 * 365) {
                endDate = DemoDate.getDate(time);
            }

        } else {
            if (l - createTime > 60 * 60 * 1000) {
                endDate =  (l - createTime) / 1000 / 60 / 60 + "小时前";
            }

            if (l - createTime < 60 * 60 * 1000) {
                endDate = (l - createTime) / 1000 / 60 + "分钟前";
            }

            if (l - createTime < 60 * 1000) {
                endDate = "刚刚";
            }
        }
        return endDate;
    }


    public static String getPostDate(long time) {
        return getPostDate(time + "");
    }

    public static long getDateTime(String date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try {
            return simpleDateFormat.parse(date).getTime();
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }



}
